

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Observable;
import java.util.Observer;
import GUI.Configuration;


public class HoldService implements Observer{
	private static WatchDir watch= WatchDir.serviceInstance;
	//private static ConnectToServer connectSer = new ConnectToServer();
	//private static Thread connect;
	public static HoldService hold = new HoldService();
	private static boolean pause = false;
	private static boolean start = true;
	private static Thread t;//= new Thread(watch);
	private static StartTag st;
	
	public HoldService() {
		//update to path of the watch directory
		try {
			String dbPath;//get the path of the directory
			XMLDecoder decoder;
			decoder = new XMLDecoder(
					new BufferedInputStream(
							new FileInputStream("c:/Users/HP/workspace/CleverDropBox/user.xml")));
			Configuration userConfig = (Configuration)decoder.readObject();
			dbPath = userConfig.getDropboxPath();
			Path dir = Paths.get(dbPath);
			watch.setDir(dir);
			String tag = userConfig.getTagPath();
			st = new StartTag(tag);
			watch.setTag(st);
			decoder.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String cmd="start";
		if(args.length > 0){ 
			cmd = args[0];
		}
		
		if("start".equals(cmd)) {
			System.out.println("START");
	    	hold.start();
	    }
	    else if("stop".equals(cmd)) {
	    	System.out.println("STOP");
			hold.stop();
		} 
	    else {
	    	hold.pause();
	    }
	}
	
	
	
	public void stop() {
		start = false;
		synchronized (this) {
			this.notify();
		}
		//watch.stopping();
		//t.interrupt();
	}

	public void pause(){
		System.out.println("PAUSE");
		
		//watch.pause();
		//t.interrupt();	
		synchronized (this) {
			//this.notify();
			//Runtime.getRuntime().getClass().notify();
		}
	}
	
	public void resume(){
		System.out.println("RESUME");
		synchronized (watch) {
			watch.resuming();
			watch.notify();
		}
	}
	
	public void start() {
	//	connect=new Thread(connectSer);
	//	connect.start();
	//	connectSer.updateServer("");
		t=new Thread(watch);
    	t.start();
    //	connect=new Thread(connectSer);
    	//connect.start();
    	
		while(start){
			System.out.println("START LOOP");
			try {
				synchronized (this) {
					System.out.println("WAITING");
					this.wait();
					System.out.println("NOT WAITING");
				}
				if(!start)
					continue;
				else if(pause==false){
					System.out.println("PAUSING");
					pause = true;
					watch.pause();
					t.interrupt();
				}
				else{
					pause = false;
					synchronized (watch) {
							watch.resuming();
							watch.notify();
					}
				}
				
			} catch (InterruptedException e) {
				
			}
		}
		watch.stopping();
		t.interrupt();
	}



	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}





	
	
	
/*
	
	@SuppressWarnings("deprecation")
	public static void readStuff(InputStream rawIn) {
	    try {
	      DataInputStream in = new DataInputStream(new BufferedInputStream(
	          rawIn));
	    	String line;
	      boolean eof = false;
	      while (!eof) {
	        try {
	          line = in.readLine();
	          System.out.println(line);
	        } catch (EOFException eofx) {
	          eof = true;
	        }
	      }

	    } catch (IOException x) {
	      x.printStackTrace();
	    }
	  }
*/
	
}
